import random

# 初始化地址存储
address_store = {}
# 初始化银行存储
bank_store = {}

def add_user_account():
    print("---------开户---------")
    username = input("请输入用户名>>")
    password = input("请设置密码(6位数字)>>")
    while not (password.isdigit() and len(password) == 6):
        password = input("密码格式错误！请重新设置6位数字密码>>")
    address = {
        "country": input("请输入国家>>"),
        "city": input("请输入省份>>"),
        "street": input("请输入街道>>"),
        "house_num": input("请输入门牌号>>")
    }
    address_store[username] = address
    print("""一类账户【金卡】：最大转账额为5万，转出最大5万，转入没限制
            二类账户【普通卡】：最大转账额为2万，转出最大2万，转入没限制""")
    account_type = input("请输入账户类型编号(1-一类账户[金卡]/2-二类账户[普通卡]) >>")

    account_info = {
        "username": username,
        "password": password,
        "address": address,
        "fund": int(input("请输入初始资金>>")),
        "account_type":account_type,
        "bank_name": "中国农业银行",
        "transaction": []  # 列表存储交易记录
    }

    # 保存账户到银行系统
    bank_store[account_number] = account_info
    print(f"恭喜您开户成功！您的账号是：{account_number}")
    return account_number

def check_bank_store(account_number):
    if len(bank_store) > 100:
        print("银行账户已满！无法执行此操作！")
        return 3
    if account_number in bank_store:
        return 2
    return 1

def random_account():
    return random.randrange(10000000,99999999)

def security_access(operation,need_password=False)->dict:
    chance = 4
    this_account = ''
    account_num = int(input(f"请输入要{operation}的账户>>"))
    while chance > 0:
        if account_num in bank_store:
            # 判断密码
            this_account = bank_store[account_num]
            while chance > 0 and need_password:
                password = input("请输入密码>>")
                if this_account["password"] != password:
                    print(f"密码错误!请重试!(剩余{chance}次机会)")
                    chance -= 1
                else:
                    break
            break
        else:
            chance -= 1
            print(f"错误！没有该{account_num}账户，请确认输入的账户是否错误!(剩余{chance}次机会)")
    if chance == 0:
        print("错误次数过多，已退出！")
    return this_account

def save_money():
    account = security_access("存钱")
    money = int(input("请输入存取的金额>>"))
    account['fund'] += money
    print(f"存取成功!账户余额:{account['fund']}")

def get_money():
    account = security_access("取钱",need_password=True)
    account_money = account['fund']
    money = int(input("请输入取款的金额>>"))
    if account_money < money:
        print(f"资金不足,无法取款！账户剩余{account_money},但希望取出{money}")
    else:
        account['fund'] -= money
        print(f"已成功取出{money}！账户剩余{account['fund']}")


def transfer_money():
    from_account = security_access("发起转账",need_password=True)
    target_account = security_access("接收转账")
    from_money = from_account['fund']
    while True:
        money = int(input("请输入取款的金额>>"))
        transfer_limit = 50000 if from_account["account_type"] == "1" else 20000
        if money > transfer_limit:
            print(f"超出转账限额！{from_account['username']}的最大转账额为{transfer_limit}元,请重试")
        else:
            break

    if from_money < money:
        print(f"资金不足,无法转账！账户剩余{from_money},但希望转出{money}")
    else:
        from_account['fund'] -= money
        target_account['fund'] += money
        print(f"已成功转出{money}给目标账户！账户剩余{from_account['fund']}")


def select_info():
    select_account = security_access("查询",need_password=True)
    print("查询结果如下：")
    user_address = address_store.get(select_account['username'], {})
    print(f"账户名称：{select_account['username']}")
    print(f"密码：{select_account['password']}")
    print(
        f"地址：国家>{user_address.get('country', '未设置')},城市>{user_address.get('city', '未设置')},街道>{user_address.get('street', '未设置')},门牌号>{user_address.get('house_num', '未设置')}")
    print(f"资金：{select_account['fund']}")
    print(f"账户类型：{select_account['account_type']}类")
    print(f"银行名称：{select_account['bank_name']}")


while True:
    print("*****************************")
    print("*\t" + "中国农业银行账户管理系统\t" + "*")
    print("*****************************")
    print("*\t\t\t" + "选项\t\t\t" + "*")
    print("*\t\t\t" + "1.开户\t\t\t" + "*")
    print("*\t\t\t" + "2.存钱\t\t\t" + "*")
    print("*\t\t\t" + "3.取钱\t\t\t" + "*")
    print("*\t\t\t" + "4.转账\t\t\t" + "*")
    print("*\t\t\t" + "5.查询\t\t\t" + "*")
    print("*\t\t\t" + "6.Bye\t\t\t" + "*")
    command = int(input("请输入操作序号>>"))
    match command:
        case 1:
            account_number = random_account()
            status = check_bank_store(account_number)
            if status == 1:
                my_account = add_user_account()
            elif status == 2:
                print("账号已存在，请重试！")
            else:
                print("银行用户已满！暂无法执行此操作!")
        case 2:
            save_money()
        case 3:
            get_money()
        case 4:
            transfer_money()
        case 5:
            select_info()
        case 6:
            print("Bye~")
            break
        case _:
            print(f"未知指令{command},不是1~5的一员.")




















